package lgq.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lgq.service.CourseManager;
import lgq.service.TrainManager;
import model.Course;
import model.Train;
import model.TrainProperty;
import model.TrainType;
import model.TrainWay;
import util.Page;

public class FindTrainServlet extends HttpServlet {
			public void doGet(HttpServletRequest request, HttpServletResponse response)
					throws ServletException, IOException {
				this.doPost(request, response);
			}
			public void doPost(HttpServletRequest request, HttpServletResponse response)
					throws ServletException, IOException {
				int pageNo=1;
				int pageSize=10;
				String keycode="";
				int idkeycode=0;
				Date starttime=null;
				Date endtime=null;
				// 如果请求存在搜索类型则从request中取，不否则从session中获取，第一次为null那么就是获取全部
				if (null != request.getParameter("serchtype")) {
					request.getSession().setAttribute("serchtype",request.getParameter("serchtype"));
				}
				// 同理判断是否有存在搜索关键之有从request获取没有从session取得
				if (null != request.getParameter("keycode")) {
					request.getSession().setAttribute("keycode",request.getParameter("keycode"));
					keycode = request.getParameter("keycode");
				} else {
					keycode = (String) request.getSession().getAttribute("keycode");
				}
				if (null != request.getParameter("idkeycode")) {
					request.getSession().setAttribute("idkeycode",Integer.parseInt(request.getParameter("idkeycode")));
					idkeycode = Integer.parseInt(request.getParameter("idkeycode"));
				} else {
					if(null!=request.getSession().getAttribute("idkeycode"))
						try{
					idkeycode = (Integer) request.getSession().getAttribute("idkeycode");
						}catch(Exception e)
						{
							idkeycode=0;
							request.getSession().setAttribute("idkeycode","0");
						}
					else
					{
						request.getSession().setAttribute("idkeycode","0");
					}
				}
				
				
				try{
					if(request.getParameter("pageNo")!=null)
					{
						pageNo=Integer.parseInt(request.getParameter("pageNo"));
					}
					if(request.getParameter("pageSize")!=null)
					{
						pageSize=Integer.parseInt(request.getParameter("pageSize"));
						
						request.getSession().setAttribute("pageSize", pageSize);
					}
					if(request.getSession().getAttribute("pageSize")!=null)
					{
						pageSize=(Integer) request.getSession().getAttribute("pageSize");
					}
				}catch(Exception e)
				{
					e.printStackTrace();
				}
				Page<Train> page=null;
				if("trainname".equals(request.getSession().getAttribute("serchtype")))
				{
					page=new  TrainManager().getByName(pageNo, pageSize,keycode);				
				}
				else if("trainType".equals(request.getSession().getAttribute("serchtype")))
				{
					TrainType tt=new TrainType();
					tt.setId(idkeycode);
					page=new TrainManager().getByTrainType(pageNo, pageSize, tt);
				}else if("trainProperty".equals(request.getSession().getAttribute("serchtype")))
				{
					TrainProperty tp=new TrainProperty();
					tp.setId(idkeycode);
					page=new TrainManager().getByProperty(pageNo, pageSize, tp);
				}
				else if("trainWay".equals(request.getSession().getAttribute("serchtype")))
				{
					TrainWay tm=new TrainWay();
					tm.setId(idkeycode);
					page=new TrainManager().getByTrainWay(pageNo, pageSize, tm);
				}
				else if("time".equals(request.getSession().getAttribute("serchtype")))
				{		
					if (null != request.getParameter("starttime")) {
						
						try {
							request.getSession().setAttribute("starttime",new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("starttime")));
							starttime = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("starttime"));
						} catch (ParseException e) {						
							e.printStackTrace();
						}					
					} else {
							starttime = (Date) request.getSession().getAttribute("starttime");
					}
					
					if (null != request.getParameter("endtime")) {
						
						try {
							request.getSession().setAttribute("endtime",new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("endtime")));
							endtime = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("endtime"));
						} catch (ParseException e) {						
							e.printStackTrace();
						}					
					} else {
						endtime = (Date) request.getSession().getAttribute("endtime");
					}
		
					page=new TrainManager().getByTime(pageNo, pageSize, starttime,endtime);
				}
				
				else
				{
					
					page=new TrainManager().getAll(pageNo, pageSize);
				}
				request.setAttribute("page", page);
				request.getRequestDispatcher("/Train/ShowTrainList.jsp").forward(request, response); 
				 
			}
}
